//
//  HTSubjectScrollView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/4.
//

import SwiftUI

struct HTRecommendScrollView: View {

  @Binding var var_selEp: HTEp!
  @Binding var var_source: String

  @State var var_animeInfo: HTAnime!
	@State var var_reco: HTRecommend = HTRecommend()
  @State var var_onSelAnime: ((HTAnime) -> Void)? = {_ in }
  @Environment(\.presentationMode) private var var_presentationMode: Binding<PresentationMode>
  @State private var var_showPushToSubject: Bool = false
  @State private var var_showPushToVideoDetail: Bool = false
  @State private var var_selAnime: HTAnime = HTAnime()

	var body: some View {
    ZStack{
      NavigationLink(destination: HTSubjectScreen(var_subId: var_reco.var_id, var_subTitle: var_reco.var_title), isActive: $var_showPushToSubject) {
        EmptyView()
      }
      NavigationLink(destination: HTVideoDetailScreen(var_id: var_selAnime.var_id, var_source: "11"), isActive: $var_showPushToVideoDetail) {
        EmptyView()
      }

      VStack(spacing: 8){
        HStack{
          Text(var_reco.var_title ?? "")
            .font(.system(size: 16))
            .fontWeight(.bold)
            .ht_normal()
          Spacer()

          if (var_reco.var_see == "1") {
            Button {
              var_showPushToSubject = true
              HTNetworkManager().ht_requestForBuried(.htEnumMoviePlayCl(var_movie_id: var_animeInfo!.var_id, var_movie_name: var_animeInfo!.var_title ?? "", var_kid: "22", var_source: var_source, var_eps_id: var_selEp.var_id ?? "", var_eps_name: var_selEp.var_title ?? "", var_movielist_id: "", var_secname: "", var_subtitle: "1", var_m_type: "2", var_ori_type: "1"))
            } label: {
              HStack(spacing: 2){
                Text("See All")
                  .font(.system(size: 14))
                  .foregroundColor(Color(hex: "#999999"))
                Image("seeall")
                  .resizable()
                  .frame(width: 14, height: 14)
              }
            }
          }
        }
        .padding(.horizontal, 15)
        ScrollView(.horizontal, showsIndicators: false) {
          HStack(spacing: 6){
            ForEach(0..<var_reco.var_animes!.count, id: \.self) { index in
              let var_anime = var_reco.var_animes![index]
              Button{
                var_onSelAnime!(var_anime)
                HTNetworkManager().ht_requestForBuried(.htEnumMoviePlayCl(var_movie_id: var_animeInfo!.var_id, var_movie_name: var_animeInfo!.var_title ?? "", var_kid: "21", var_source: var_source, var_eps_id: var_selEp.var_id ?? "", var_eps_name: var_selEp.var_title ?? "", var_movielist_id: "", var_secname: "", var_subtitle: "1", var_m_type: "2", var_ori_type: "1"))
              } label: {
                HTSubjectItem(var_anime: var_anime, var_showDUB: false)
              }
            }
            if (var_reco.var_see == "1") {
              NavigationLink {
                HTSubjectScreen(var_subId: var_reco.var_id, var_subTitle: var_reco.var_title)
              } label: {
                HStack(){
                  Image("more")
                    .frame(width: 22, height: 22)
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
              }
              .background(Color.white)
              .frame(width: 106)
              .cornerRadius(6)
            }
          }
          .padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 15))
        }
        .frame(maxWidth: .infinity)
      }
    }
	}
}
